<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author ThinkGem
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <!-- 表格标题 -->
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <!-- 表格右上角自定义按钮（新增...） -->
      <template #toolbar>
        <a-button type="primary" @click="lxjgAdd()">
          <Icon icon="fluent:add-12-filled" /> {{ t('添加') }}
        </a-button>
      </template>
      <template #taskCode="{ record }">
        <div :style="props.task.taskCode == record.task.taskCode ? { color: 'red' } : ''">
          （{{record.task.taskCode}}）{{ record.orderPic.picno + '-' +record.orderPic.picname}}
        </div>
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsTestTestDataList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, watch, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  // import { testDataDelete, testDataListData } from '/@/api/test/testData';
  // import { testDataDisable, testDataEnable } from '/@/api/test/testData';
  // import { useDrawer } from '/@/components/Drawer';
  // import { FormProps } from '/@/components/Form';
  import { lxListData, tasklxList, lxjgSave, lxjgDelete } from '/@/api/m8/machine/task';
  // import { ListSelect } from '/@/components/ListSelect';
  // const selectListRef = ref<Recordable[]>([]);
  // const listSelectRef = ref<any>(null);
  // const onSelect = (values: Recordable[]) => {
  //   selectListRef.value = values;
  // };
  const { t } = useI18n('test.testData');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('数据管理'),
  };
  const props = defineProps({
    task: { type: Object, default: {} },
  });
  watch(
    () => props.task,
    async () => {
      const res = await tasklxList({ taskId: props.task.taskCode });
      props.task.ckey = res.taskLxjg.ckey;
      setProps({
        formConfig: {
          baseColProps: { lg: 6, md: 8 },
          labelWidth: 90,
          schemas: [
            {
              label: t('任务'),
              field: 'task.taskCode',
              component: 'Input',
            },
            {
              label: t('图纸'),
              field: 'orderPic.picno',
              component: 'Input',
            },
            {
              label: t('名称'),
              field: 'orderPic.picname',
              component: 'Input',
            },
            {
              label: t('任务选择'),
              field: 'task.lxjgCode',
              component: 'ListSelect',
              show: false,
              componentProps: {
                selectType: 'taskSelect',
                params: { selIds: props.task.taskCode, lxjgKey: props.task.ckey },
                checkbox: true,
                onChange: (taskCode) => {
                  // console.log(userCode,'112233');
                  lxSave(taskCode);
                },
              },
            },
          ],
        },
      });
      await reload();
      // console.log(props.task, 'watch====');
      // setTimeout(() => {
      //   reload();
      // });
    },
    { immediate: true },
  );
  //配置表单内容
  // const searchForm: FormProps = {
  //   baseColProps: { lg: 6, md: 8 },
  //   labelWidth: 90,
  //   schemas: [
  //     {
  //       label: t('任务'),
  //       field: 'task.taskCode',
  //       component: 'Input',
  //     },
  //     {
  //       label: t('图纸'),
  //       field: 'orderPic.picno',
  //       component: 'Input',
  //     },
  //     {
  //       label: t('名称'),
  //       field: 'orderPic.picname',
  //       component: 'Input',
  //     },
  //     {
  //       label: t('任务选择'),
  //       field: 'task.lxjgCode',
  //       component: 'ListSelect',
  //       componentProps: {
  //         selectType: 'taskSelect',
  //         params: { selIds: props.task.taskCode, lxjgKey: props.task.ckey },
  //         checkbox: true,
  //         // onChange: (userCode) => {
  //         //   loadData({ userCode });
  //         // },
  //       },
  //     },
  //   ],
  // };
  //配置表格表头菜单
  const tableColumns: BasicColumn[] = [
    {
      title: t('任务'),
      dataIndex: 'task.taskCode',
      key: 'task.oper_code',
      sorter: true,
      width: 260,
      align: 'left',
      slot: 'taskCode',
    },
    {
      title: t('任务状态'),
      dataIndex: 'task.taskStatus',
      key: 'task.task_status',
      sorter: true,
      width: 100,
      align: 'center',
      // slot: 'firstColumn',
      dictType: 'm8_task_status',
    },

    {
      title: t('加工顺序'),
      dataIndex: 'task.sortNum',
      key: 'task.sort_num',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('优先级'),
      dataIndex: 'task.firstLevel',
      key: 'task.first_level',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('数量'),
      dataIndex: 'task.iqty',
      key: 'task.iqty',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('计划开始'),
      dataIndex: 'task.planBeginDate',
      key: 'task.plan_begin_date',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('计划截止'),
      dataIndex: 'task.planEndDate',
      key: 'task.plan_end_date',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('加工'),
      dataIndex: 'task.jgtime',
      key: 'task.jgtime',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('调试'),
      dataIndex: 'task.tstime',
      key: 'task.tstime',
      sorter: true,
      width: 80,
      align: 'center',
    },
  ];
  //配置表格右边操作按钮
  const actionColumn: BasicColumn = {
    width: 100,
    actions: (record: Recordable) => [
      // {
      //   icon: 'clarity:timeline-line',
      //   title: t('页签方式编辑'),
      //   onClick: handleForm2.bind(this, { id: record.id }),
      //   auth: 'm8:machine:task:edit',
      // },
      // {
      //   icon: 'clarity:note-edit-line',
      //   title: t('编辑数据'),
      //   onClick: handleForm.bind(this, { id: record.id }),
      //   auth: 'm8:machine:task:edit',
      // },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除数据'),
        popConfirm: {
          title: t('是否确认删除数据'),
          confirm: handleDelete.bind(this, { taskId: record.taskId }),
        },
      },
    ],
  };

  // const [registerDrawer, { openDrawer }] = useDrawer();
  // const [registerDrawer2, { openDrawer: openDrawer2 }] = useDrawer();
  const [registerTable, { reload, setProps }] = useTable({
    api: lxListData,
    beforeFetch: (params) => {
      params.ckey = props.task.ckey;
      return params;
    },
    columns: tableColumns, //配置表格内容数组对象
    actionColumn: actionColumn, //配置表格右边操作按钮
    // formConfig: searchForm, //配置表单内容
    showTableSetting: true, //表格右上角3个默认按钮
    useSearchForm: true, //表单是否展示
    canResize: true, //表格是否flex布局
    immediate: false,
    resizeHeightOffset:50
  });

  // function handleForm(record: Recordable) {
  //   openDrawer(true, record);
  // }

  async function lxSave(record) {
    const params: any = {
      selIds: record,
      curTask: props.task.taskCode,
      ckey: props.task.ckey,
    };

    const res = await lxjgSave(params);
    showMessage(res.message); // 显示提交结果
    await reload();
  }

  function lxjgAdd() {
    // console.log(props.task, '1111');

    console.log(document.getElementById('form_item_task.lxjgCode')?.click());
  }

  // async function handleDisable(record: Recordable) {
  //   const res = await testDataDisable(record);
  //   showMessage(res.message);
  //   handleSuccess();
  // }

  // async function handleEnable(record: Recordable) {
  //   const res = await testDataEnable(record);
  //   showMessage(res.message);
  //   handleSuccess();
  // }

  async function handleDelete(record: Recordable) {
    const res = await lxjgDelete(record);
    showMessage(res.message);
    handleSuccess();
  }
  // function handleTestDataChildRowClick(record: Recordable) {
  //   record.onEdit?.(true, false);
  // }
  function handleSuccess() {
    reload();
  }
</script>
